﻿#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/config/SingleWindow>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/ShapeDrawable>

#include <iostream>

/*
class VisitorNodePath : public osg::NodeVisitor
{
public:
	VisitorNodePath() : osg::NodeVisitor(TRAVERSE_PARENTS)
	{

	}

	void apply(osg::Node& node)
	{
		std::cout << "Apply node: " << node.getName() << std::endl;
		traverse(node);
	}

	void apply(osg::Group& group)
	{
		std::cout << "Apply group: " << group.getName() << std::endl;
		traverse(group);
		if (group.getName() == "")
		{
			std::cout << group.className() << std::endl;
		}
	}

	void apply(osg::MatrixTransform& mt)
	{
		std::cout << "Apply mtransform: " << mt.getName() << std::endl;
		traverse(mt);
	}
};
*/

osg::ref_ptr<osg::Geode> CreateBox(osg::Vec3Array* va)
{
	osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
	for (osg::Vec3Array::iterator iter = va->begin(); iter != va->end(); iter++)
	{
		gnode->addDrawable(new osg::ShapeDrawable(
			new osg::Box(osg::Vec3(iter->x(), iter->y(), iter->z()),
			0.01, 0.01, 0.01)));
	}

	return gnode;
}

class BoundVisitor : public osg::NodeVisitor
{
public:
	void apply(osg::Geode& gnode)
	{
		// 输出所有顶点
		for (int i = 0; i < gnode.getNumDrawables(); i++)
		{
			osg::ref_ptr<osg::Geometry> gm = dynamic_cast<osg::Geometry*>(gnode.getDrawable(i));
			osg::Vec3Array *vx = dynamic_cast<osg::Vec3Array*>(gm->getVertexArray());
			// for (osg::Vec3Array::iterator iter = vx->begin(); iter != vx->end(); iter++)
			{
				if (group.valid())
				{
					std::cout << group->getName() << std::endl;
					if ("root" == group->getName())
					{
						group->addChild(CreateBox(vx));
					}
				}
				//std::cout << iter->x() << "    :    " << iter->y() << "    :    " << iter->z() << std::endl;
			}
		}
	}
	void setGroup(osg::Group* gp)
	{
		group = gp;
	}
private:
	osg::ref_ptr<osg::Group> group;
};

int main()
{
	/*
	osg::ref_ptr<osgViewer::Viewer>				viewer	= new osgViewer::Viewer;
	osg::ref_ptr<osg::Group>					root	= new osg::Group;
	osg::ref_ptr<osg::Node>						glider	= osgDB::readNodeFile("glider.osg");
	osg::ref_ptr<osg::Node>						cow		= osgDB::readNodeFile("cow.osg");
	osg::ref_ptr<osg::MatrixTransform>			child1	= new osg::MatrixTransform;
	osg::ref_ptr<osg::MatrixTransform>			child2	= new osg::MatrixTransform;
	osg::ref_ptr<osg::MatrixTransform>			child3 = new osg::MatrixTransform;
	osg::ref_ptr<osg::MatrixTransform>			child4 = new osg::MatrixTransform;
	osg::ref_ptr<osg::MatrixTransform>			child5 = new osg::MatrixTransform;
	VisitorNodePath vn;

	root->setName("root");
	glider->setName("glider");
	cow->setName("cow");
	root->addChild(glider);	// root->glider
	root->addChild(child1);
	root->addChild(child2);
	
	child1->setName("child1");
	child1->setMatrix(osg::Matrix::translate(-5, -5, 0));
	child1->addChild(glider);
	child1->addChild(child3);
	child1->addChild(child4);

	child2->setName("child2");
	child2->setMatrix(osg::Matrix::translate(5, -5, 0));
	child2->addChild(glider);
	child2->addChild(child5);

	child3->setName("child3");
	child3->setMatrix(osg::Matrix::translate(-5, -5, 0));
	child3->addChild(cow);

	child4->setName("child4");
	child4->setMatrix(osg::Matrix::translate(5, -5, 0));
	child4->addChild(cow);

	child5->setName("child5");
	child5->setMatrix(osg::Matrix::translate(5, -5, 0));
	child5->addChild(cow);

	viewer->setSceneData(root);
	viewer->apply(new osgViewer::SingleWindow(100, 100, 800, 600, 0));
	cow->accept(vn);

	return viewer->run();
	*/

	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	osg::ref_ptr<osg::Node> glider = osgDB::readNodeFile("glider.osg");
	BoundVisitor bv;
	osg::ref_ptr<osg::Group> group = new osg::Group;
	group->setName("root");
	bv.setGroup(group.get());
	glider->accept(bv);
	
	group->addChild(glider);

	viewer->setSceneData(group);
	viewer->setUpViewInWindow(100, 100, 800, 600);
	return viewer->run();

}
